package com.example.dataservice.serviceimpl;

import com.example.common.constants.YLBConstant;
import com.example.common.util.CommonUtil;
import com.example.dataservice.mapper.BidInfoMapper;
import com.example.dataservice.mapper.FinanceAccountMapper;
import com.example.dataservice.mapper.ProductInfoMapper;
import com.example.dataservice.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.model.BidInfo;
import org.example.api.model.FinanceAccount;
import org.example.api.model.ProductInfo;
import org.example.api.pojo.BidProductInfo;
import org.example.api.pojo.UserBidInfo;
import org.example.api.service.InvestService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/14 下午 06:13
 * @version: v1.0
 */
@DubboService(interfaceClass = InvestService.class, version = "1.0")
public class InvestServiceImpl implements InvestService{
    @Resource
    private BidInfoMapper bidInfoMapper;
    @Resource
    private FinanceAccountMapper financeAccountMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    /*根据产品的id，查询投资信息*/
    public List<BidProductInfo> queryBidProductInfo(Integer productId,
                                                    Integer pageNo,
                                                    Integer pageSize) {
        List<BidProductInfo> bidList = new ArrayList<>();
        if (productId != null && productId > 0){
            pageNo = CommonUtil.defaultPageNO(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            int offset = (pageNo -1) * pageSize;
            bidList = bidInfoMapper.selectBidInfoByProductId(productId, offset, pageSize);
        }
        return bidList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    /*投资理财产品*/
    public int investProduct(Integer uid, Integer productId, BigDecimal money) {
        int result = 0;
        if ((uid != null && uid > 0) && (productId != null &&productId >0)
                && (money !=null && money.doubleValue() %100 == 0 && money.intValue() >= 100)){
            // 1.检查用户资金   (locked)
            FinanceAccount account = financeAccountMapper.selectByUserId(uid);
            if (account != null){
                if (CommonUtil.get(account.getAvailableMoney(), money)){
                    // 2.检查产品的是否可以购买
                    ProductInfo productInfo = productInfoMapper.selectByPrimaryKey(productId);
                    if (productInfo != null && productInfo.getProductStatus() == YLBConstant.PRODUCT_STATUS_SELLING){
                        // 3.检查产品余额
                        if (CommonUtil.get(productInfo.getLeftProductMoney(),money) &&
                        CommonUtil.get(money,productInfo.getBidMinLimit())&&
                        CommonUtil.get(productInfo.getBidMaxLimit(), money)){
                            // 4.用户资金更新
                            BigDecimal availableMoney = account.getAvailableMoney().subtract(money);
                            account.setAvailableMoney(availableMoney);
                            int count = financeAccountMapper.updateByPrimaryKeySelective(account);
                            if (count < 1){
                                throw new RuntimeException("投资账户资金更新失败");
                            }
                            // 5.产品余额更新
                            BigDecimal leftProductMoney = productInfo.getLeftProductMoney().subtract(money);
                            if (leftProductMoney.compareTo(new BigDecimal(0)) == 0){
                                // 5.1 更新产品状态
                                productInfo.setProductStatus(YLBConstant.PRODUCT_STATUS_SELLED);
                                // 5.2 更新满标时间
                                productInfo.setProductFullTime(new Date());
                            }
                            productInfo.setLeftProductMoney(leftProductMoney);
                            count = productInfoMapper.updateByPrimaryKeySelective(productInfo);
                            if (count < 1){
                                throw new RuntimeException("产品余额更新失败");
                            }
                            // 6.用户投资记录
                            BidInfo bidInfo = new UserBidInfo();
                            bidInfo.setUid(uid);
                            bidInfo.setProdId(productId);
                            bidInfo.setBidMoney(money);
                            bidInfo.setBidStatus(YLBConstant.BID_STATUS_SUCC);
                            bidInfo.setBidTime(new Date());
                            bidInfoMapper.insertSelective(bidInfo);

                            // 投资成功
                            result = 1;
                        }

                        // 排行榜更新
                    }else {
                        // 产品不可以购买
                        result = 4;
                    }
                }else{
                    // 用户资金不满足
                    result = 3;
                }
            }else {
                // 账号不存在
                result = 2;
            }
        }
        return result;
    }
}
